<template>
	<div>
		<view class='flash-sale'>
			<view class='iconfont icon-xiangzuo' @tap='goBack' :style="'top:'+ (navH/2) +'rpx'" v-if="returnShow">
			</view>
			<view class='head'>
				<image src="/pages/match/static/images/ss_top_bg00.png" style="width:100%"></image>
				
			</view>
		</view>
		<view class="matchbox">
			<view class="matchTabs">
				<view class="matchTabsItem " :class="active===index?'active':''" v-for="(item,index) of tabs"
					:key="index" @click="changeTab" :data-index="index">
					<view class="matchTabsTitle">
						{{item.name}}
					</view>
					<view class="matchTabsline">
					</view>
				</view>

			</view>
			<view class="matchList">
				<view v-for="(item,index) of matchlist" :key="index" class="matchitem" @click="matchDetail" :data-id="item.id">
					<image class="matchposter" :src="item.cover_image" mode=""></image>
					<view :class="active===0?'matchwillbox':active===1?'matchbingbox':active===2?'matchendbox':''">
						<view class="matchtt">
							<view class="matchtitle">
								{{item.name}}
							</view>
							<view v-if="active===0" class="matchtime">
								<!-- 倒计时: 02天 12时 36分 56秒 -->
								
								报名截止时间:{{item.deadline_time}}
							</view>
						</view>
						<view v-if="active===0" class="matchconbtn" :class="item.is_apply === 2 ? 'yijieshu':''"> 
							{{item.is_apply === 2 ? '报名截止':'报名'}}
						</view>
						<view v-if="active===2" class="matchconbtn yijieshu">
							已结束
						</view> 
					</view>
				</view>
			</view>
			<view class="">
				
			</view>
			<view v-if="isfinish && matchlist.length===0" class="empty">
				<image src="/static/images/no-shop.png" mode=""></image>
				<view class="emptytext">
					暂无相关分类赛事
				</view>
			</view>

		</view>
		<home></home>
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
	</div>
</template>

<script>
	// +----------------------------------------------------------------------
	// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
	// +----------------------------------------------------------------------
	// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
	// +----------------------------------------------------------------------
	// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
	// +----------------------------------------------------------------------
	// | Author: CRMEB Team <admin@crmeb.com>
	// +----------------------------------------------------------------------
	import {
		contestlist
	} from '../../../api/match.js';
	import {
		mapGetters
	} from "vuex";
	
	import authorize from '@/components/Authorize';
	import {
		toLogin
	} from '@/libs/login.js';
	import home from '@/components/home/index.vue'
	import tabNav from '@/components/combinNav'
	import userNameSwiper from '@/components/userNameSwiper/userNameSwiper.vue'
	import {
		getIndexData
	} from '@/api/api.js';
	let app = getApp();
	export default {
		components: {
			home,
			tabNav,
			userNameSwiper,
			authorize
		},
		data() {
			return {
				navH: '',
				topImage: '',
				tabs: [{
						name: "未开始",
						type: 2
					},
					{
						name: "进行中",
						type: 0
					},
					{
						name: "已结束",
						type: 1
					}
				],
				active: 0,
				type: 0,
				scrollLeft: 0,
				interval: 0,
				status: 1,
				page: 1,
				limit: 10,
				isFixed: false,
				navIndex: 0,
				returnShow: true,
				matchlist: [],
				isfinish: false,
				isempty: false,
				isbottom: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
			}
		},
		computed: mapGetters(['isLogin']),
		onShow() {
			this.getmatchlist('resetlist')
			// if (this.isLogin) {
			// 	this.getmatchlist('resetlist')
			// } else {
			// 	this.isAuto = true;
			// 	this.$set(this, 'isShowAuth', true);
			// }
		},
		
		
		// onShow(){
		// 	this.getmatchlist('resetlist')
		// },
		/**
		 * 用户点击右上角分享
		 */
		// #ifdef MP
		onShareAppMessage: function() {
			
		},
		
		onShareTimeline: function() {
			return {
				title: '赛事',
				query: {
					key: ''
				},
				imageUrl: ''
			}
		},
		// #endif
		onReady() {
			var pages = getCurrentPages();
			this.navH = app.globalData.navHeight;
		},
		

		methods: {
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			onLoadFun() {
				this.isShowAuth = false;
				this.getmatchlist('resetlist')
			},
			// 获取比赛列表
			async getmatchlist(resetlist) {
				console.log(234)
				if(resetlist==="resetlist"){
					this.page = 1
					this.isfinish = false
					this.matchlist = []
				}
				if(this.isfinish){
					return false
				} 
				var data = {
					page: this.page,
					limit: this.limit,
					type: this.tabs[this.active].type
				}
				uni.showLoading({})
				var res = await contestlist(data)
				uni.hideLoading()
				this.page = this.page+1
			    if(res.data.list.length===0 && this.matchlist.length===0){
					return this.isfinish = true
				}
				if(res.data.list.length<this.limit){
					 this.isfinish = true
				}
				this.matchlist = this.matchlist.concat(...res.data.list)
				console.log(res)
			},
			matchDetail(e) {
				var {id }= e.currentTarget.dataset
				uni.navigateTo({
					url: '/pages/match/matchDetail/index?id='+id
				})
			},
			goBack: function() {
				console.log('fanhui')
				// uni.navigateBack({delta:1});
				uni.switchTab({
					url: "/pages/index/index"
				})

			},
			// 导航分类切换
			changeTab(e) {
				let that = this
				var index = e.currentTarget.dataset.index
				if (that.active == e.index) return
				that.active = index;
				this.getmatchlist('resetlist')
			},
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getmatchlist()
		},
		// 滚动监听
		onPageScroll(e) {
			// #ifdef H5
			let self = this
			if (e.scrollTop >= self.searchH) {
				self.isFixed = true
			} else {
				self.isFixed = false
			}
			// #endif
		}
	}
</script>

<style lang="scss">
	page {
		background: #F87040;
	}

	.icon-xiangzuo {
		font-size: 40rpx;
		color: #fff;
		position: fixed;
		left: 30rpx;
		z-index: 99;
		transform: translateY(-20%);

	}

	.head {
		/* #ifdef MP */
		height: 600rpx;

		/* #endif */
		/* #ifdef H5 */
		height: 490rpx;

		/* #endif */

		background-repeat: no-repeat;
		background-size: 100% 100%;
		width: 100%;

		image {
			width: 100%;
			height: 100%;
		}

	}

	.matchbox {
		.matchTabs {
			width: 682rpx;
			height: 83rpx;
			background: #FFFFFF;
			box-shadow: 0px 5px 5px 0px rgba(7, 1, 2, 0.1);
			border-radius: 40rpx;
			margin: auto;
			box-sizing: border-box;
			display: flex;

			.matchTabsItem {
				width: 33.333%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.matchTabsTitle {
					height: 26rpx;
					font-size: 26rpx;
					font-family: SourceHanSansCN;
					font-weight: bold;
					color: #666;
					line-height: 33rpx;
					text-align: center;

				}

				.matchTabsline {
					width: 75rpx;
					height: 2rpx;
					background: #fff;
					margin-top: 10rpx;
				}
			}

			.active {
				.matchTabsTitle {
					color: #EB5139;
				}

				.matchTabsline {
					background: #EB5139;
				}
			}
		}

		.matchList {
			width: 683rpx;
			border-radius: 20rpx;
			margin: auto;
			padding-bottom: 100rpx;

			.matchitem {
				width: 100%;
				box-sizing: border-box;
				padding: 28rpx 32rpx;
				margin-top: 30rpx;
				background: #FFFFFF;
				border-radius: 20rpx;

				.matchposter {
					width: 620rpx;
					height: 260rpx;
					border-radius: 15rpx;
				}

				.matchwillbox {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 30rpx;

					.matchtt {
						.matchtitle {
							width: 420rpx;
							font-size: 26rpx;
							font-family: SourceHanSansCN;
							font-weight: 500;
							color: #000000;
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;
						}

						.matchtime {
							width: 420rpx;
							font-size: 24rpx;
							font-family: SourceHanSansCN;
							font-weight: 500;
							color: #EE3C30;
							margin-top: 20rpx;
						}
					}

					.matchconbtn {
						width: 145rpx;
						height: 62rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 26rpx;
						line-height: 72rpx;
						background: url(../static/images/actbtn.png);
						background-size: 100%;
					}

					.matchconbtn.yijieshu {
						background: url(../static/images/actbtn2.png);
						background-size: 100%;
					}
				}

				.matchbingbox {
					width: 420rpx;
					font-size: 26rpx;
					font-family: SourceHanSansCN;
					font-weight: 500;
					color: #000000;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					text-align: center;
					margin: auto;
					margin-top: 20rpx;
				}

				.matchendbox {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 30rpx;

					.matchtt {
						.matchtitle {
							width: 420rpx;
							font-size: 26rpx;
							font-family: SourceHanSansCN;
							font-weight: 500;
							color: #000000;
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;
						}

						.matchtime {
							width: 420rpx;
							font-size: 24rpx;
							font-family: SourceHanSansCN;
							font-weight: 500;
							color: #EE3C30;
							margin-top: 20rpx;
						}
					}

					.matchconbtn {
						width: 145rpx;
						height: 62rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 26rpx;
						line-height: 72rpx;
						background: url(~pages/activity/static/images/combin_buy.png);
						background-size: 100%;
					}

					.matchconbtn.yijieshu {
						background: url(../static/images/actbtn2.png);
						background-size: 100%;
					}
				}
			}
		}
	}
	.empty{
		text-align: center;
		position:relative;
		image{
			width:400rpx;
		}
		.emptytext{
			position:absolute;
			bottom:100rpx;
			left:0;
			right:0;
			margin:auto;
			color:#fff;
			text-align:center;
		}
	}
</style>
